package com.lin.manager.controller.system;

import com.lin.manager.aop.Log;
import com.lin.manager.common.enums.LogEnum;
import com.lin.manager.common.req.QueryPage;
import com.lin.manager.common.resp.R;
import com.lin.manager.system.entity.SysUserEntity;
import com.lin.manager.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者: 林江
 * @创建时间: 2024-03-13 11:48:25
 * @功能:
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("user")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @ApiOperation("用户管理 - 分页查询")
    @Log(module = "用户管理", descipt = "分页查询", type = LogEnum.SELECT)
    @GetMapping("listPage")
    public R listPage(QueryPage page) {
        return R.ok(sysUserService.listPage(page));
    }

    @ApiOperation("用户管理 - 添加")
    @Log(module = "用户管理", descipt = "添加", type = LogEnum.INSERT)
    @PostMapping("add")
    public R add(@RequestBody SysUserEntity sysUser) {
        sysUserService.add(sysUser);
        return R.ok();
    }

    @ApiOperation("用户管理 - 修改")
    @Log(module = "用户管理", descipt = "修改", type = LogEnum.UPDATE)
    @PostMapping("update")
    public R update(@RequestBody SysUserEntity sysUser) {
        sysUserService.update(sysUser);
        return R.ok();
    }

    @ApiOperation("用户管理 - 删除")
    @Log(module = "用户管理", descipt = "删除", type = LogEnum.DELETE)
    @DeleteMapping("delete")
    public R del(String id) {
        sysUserService.del(id);
        return R.ok();
    }

    @ApiOperation("用户管理 - 详情")
    @Log(module = "用户管理", descipt = "详情", type = LogEnum.SELECT)
    @GetMapping("getById/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(sysUserService.getById(id));
    }

    @ApiOperation("用户管理 - 查询用户关联的角色")
    @Log(module = "用户管理", descipt = "查询用户关联的角色", type = LogEnum.SELECT)
    @GetMapping("/relative/{userId}")

    public R relative(@PathVariable("userId") Long userId) {
        return R.ok(sysUserService.relative(userId));
    }

    @ApiOperation("用户管理 - 绑定角色")
    @Log(module = "用户管理", descipt = "绑定角色", type = LogEnum.INSERT)
    @PostMapping("/binding/{userId}")
    public R binding(@PathVariable("userId") Long userId, @RequestBody List<Integer> roleIds) {
        sysUserService.binding(userId, roleIds);
        return R.ok();
    }
}
